package net.sea.setup.ui.menu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.context.AppContext;
import net.sea.setup.config.Page;
import net.sea.setup.config.Wizard;

/**
 * 向导菜单页面
 *
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class MenuPanel extends JPanel {
	// 获取上下文实例对象
	private static AppContext contextInst = AppContext.getInstance();

	/**
	 * 创建向导菜单
	 */
	public MenuPanel() {
		// 获取系统的配置
		Wizard wizard = (Wizard) contextInst
				.getAttribute(AppConstants.CFG_WIZARD_KEY);
		if (wizard == null) {
			throw new RuntimeException("系统配置错误……");
		}
		setBorder(new TitledBorder(new LineBorder(Color.GRAY),
				wizard.getMenuTitle(), TitledBorder.LEADING, TitledBorder.TOP,
				null, null));
		setLayout(new BorderLayout());
		// logo容器
		JPanel logoPanel = new JPanel();
		logoPanel.setLayout(new BorderLayout());
		JLabel lb = new JLabel();
		lb.setOpaque(false);
		logoPanel.add(BorderLayout.WEST, lb);
		logoPanel.setOpaque(false);
		add(BorderLayout.NORTH, logoPanel);

		// 内容容器
		addContentPanel(wizard);
	}

	/**
	 * 添加内容容器
	 *
	 * @param wizard
	 *            系统的配置
	 */
	private void addContentPanel(Wizard wizard) {
		List<Page> pages = wizard.getPages();
		JPanel contentPanel = new JPanel();
		// 创建布局
		GridBagLayout gridBagLayout = new GridBagLayout();
		int size = pages.size() * 2 + 1;
		int[] rowHeights = new int[size];
		double[] rowWeights = new double[size];
		for (int i = 0; i < size; i++) {
			if (i == 0) {
				rowHeights[i] = 20;
			} else if (i % 2 == 0) {
				rowHeights[i] = 15;
			} else {
				rowHeights[i] = 0;
			}
			if (i < size - 1) {
				rowWeights[i] = 0.0;
			} else {
				rowWeights[i] = Double.MIN_VALUE;
			}
		}

		gridBagLayout.columnWidths = new int[] { 10, 0, 0, 0 };
		gridBagLayout.rowHeights = rowHeights;
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = rowWeights;
		contentPanel.setLayout(gridBagLayout);

		List<JLabel> lables = new ArrayList<JLabel>();
		for (int i = 0; i < pages.size(); i++) {
			Page page = pages.get(i);

			// 图标标签
			JLabel iconLb = new JLabel();
			if (i == 0) {
				iconLb.setIcon(new ImageIcon(MenuPanel.class
						.getResource("/img/installing.gif")));
			}
			GridBagConstraints iconGbc = new GridBagConstraints();
			iconGbc.anchor = GridBagConstraints.EAST;
			iconGbc.insets = new Insets(0, 0, 5, 5);
			iconGbc.gridx = 1;
			iconGbc.gridy = 2 * i + 1;
			contentPanel.add(iconLb, iconGbc);

			// 目录标签
			JLabel menuLb = new JLabel(page.getMenu().getText());
			GridBagConstraints menuGbc = new GridBagConstraints();
			menuGbc.anchor = GridBagConstraints.WEST;
			menuGbc.insets = new Insets(0, 0, 5, 0);
			menuGbc.gridx = 2;
			menuGbc.gridy = 2 * i + 1;
			contentPanel.add(menuLb, menuGbc);
			lables.add(iconLb);
		}
		contentPanel.setOpaque(false);
		add(BorderLayout.CENTER, contentPanel);
		// 把向导菜单存到系统上下文中
		contextInst.setAttribute(AppConstants.WIZARD_MENU_KEY, lables);
	}
}
